package com.smart.rpc.hessian;

import java.text.MessageFormat;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.remoting.caucho.HessianServiceExporter;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.smart.register.service.IServiceRegister;
import com.smart.register.service.ServiceConfig;
import com.smart.register.service.zookeeper.ZkServiceRegister;
import com.smart.rpc.annotation.HessianService;
import com.smart.rpc.annotation.RegisterService;

@Component
public class HessianServiceExport implements BeanFactoryAware {

	private DefaultListableBeanFactory factory;
	
	
	public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
		factory = (DefaultListableBeanFactory)beanFactory;
		exportServices();
	}
	
	private void exportServices(){
		//获取所有注解为HessianService的bean名称
		String[] beanNames = factory.getBeanNamesForAnnotation(HessianService.class);
		for(String beanName : beanNames){
			//通过bean名称获取对象实例
			Object bean = factory.getBean(beanName);
			//获取对象类型并获取注解值
			Class<?> clazz = bean.getClass();
			/*
			 * 获取对外提供的Hessian接口
			 * 默认将实现的第1个接口作为发布接口
			 * TODO 可以通过HessianService注解进行配置(未实现)
			 */
			Class<?>[] intfClazzs = clazz.getInterfaces();
			HessianService hessainInfo = clazz.getAnnotation(HessianService.class);
			
			//处理服务名称
			String serviceName = hessainInfo.value();
			serviceName = getServiceName(serviceName, intfClazzs[0].getSimpleName());
			
			//导出Hessain服务
			boolean result = this.exportHessain(serviceName, bean, intfClazzs[0]);
			if(result){
				/*
				 * 标记服务注册
				 * 默认Zookeeper为注册中心
				 */
				RegisterService registerService = clazz.getAnnotation(RegisterService.class);
				//服务注册
				if(registerService != null){
					registerCenter(serviceName);
				}
			}
		}
	}
	
	private boolean exportHessain(String serviceName, Object bean, Class<?> intfClazz){
		System.out.println(MessageFormat.format("Export Service Name : {0}", serviceName));
		if(!factory.containsSingleton(serviceName)){
			HessianServiceExporter hessianServiceExporter = new HessianServiceExporter();
			hessianServiceExporter.setServiceInterface(intfClazz);
			hessianServiceExporter.setService(bean);
			hessianServiceExporter.afterPropertiesSet();
			factory.registerSingleton(serviceName, hessianServiceExporter);
		}
		return true;
	}

	private String getServiceName(String serviceName, String intfName){
		/*
		 * 如果未设定发布服务的名称,则默认采用接口的名称
		 */
		if(!StringUtils.hasText(serviceName)){
			serviceName = "/".concat(intfName);
		}else if(!serviceName.startsWith("/")){
			serviceName = "/".concat(serviceName);
		}
		
		return serviceName;
	}
	
	private void registerCenter(String serviceName){
		System.out.println(">> 向注册中心注册服务...");
		IServiceRegister register = new ZkServiceRegister();
		ServiceConfig config = new ServiceConfig();
		config.setUri(serviceName);
		register.registerService(config);
		
		
	}
}
